extends Control

onready var imgs = [
	get_node("image0"),
	get_node("image1")
]

var _imgFile = ''
var _effectId = 0
var _effectDuration = 0
var _moveByPoint:Vector2
var _scaleToVector:Vector2
var _rotateByValue:float

func _ready():
	var tween = $image1/tween
	tween.connect("tween_all_completed", self, "_fadeInFinish")
	pass

func type():
	return "sprite"

func loadImage(fileName : String):
	_imgFile = fileName
	if _hasNoSprite():
		_setTexture(0)
	else:
		_setTexture(1)
	setEffect(0, 0)
	pass
	
func nullImage():
	imgs[0].set_texture(null)
	imgs[1].set_texture(null)
	loadImage('')
	setVisible(false)
	pass
	
func setVisible(v : bool):
	self.visible = v
	pass
	
func setPosition(pos : Vector2):
	self.rect_position = pos
	pass
	
func setEffect(number : int, ms : int):
	# 1 fade in
	# 2 fade out
	# 3 shake
	_effectId = number
	_effectDuration = ms
	match _effectId:
		1:
			_effectFadeIn()
		2:
			_effectFadeOut()
		3:
			_effectShake()
		4:
			_moveBy()
		5:
			_scaleTo()
		6:
			_rotateBy()
	pass
	
func moveBy(v : Vector2, ms : int):
	_moveByPoint = v
	setEffect(4, ms)
	
func scaleTo(v : Vector2, ms : int):
	_scaleToVector = v
	setEffect(5, ms)
	
func rotateBy(v : float, ms : int):
	_rotateByValue = v
	setEffect(6, ms)
	
func _setTexture(index : int):
	if _imgFile.length() == 0:
		return
	var texture = load(_imgFile)
	if texture == null:
		print("图片[{name}]加载失败.".format({
			"name": _imgFile
		}))
		return
	imgs[index].set_texture(texture)
	if index == 1:
		imgs[index].visible = false
	
func _hasNoSprite():
	return imgs[0].texture == null
	
func _hasOneSprite():
	return imgs[0].texture != null && imgs[1].texture == null
	
func _hasTwoSprite():
	return imgs[0].texture != null && imgs[1].texture != null
	
func _effectFadeIn():
	if _hasOneSprite():
		var tween = $image0/tween
		tween.interpolate_property(imgs[0], "self_modulate", Color(1, 1, 1, 0), Color(1, 1, 1, 1), _effectDuration / 1000.0)
		tween.start()
		pass
	elif _hasTwoSprite():
		imgs[1].visible = true
		imgs[1].self_modulate = Color(1, 1, 1, 0)
		var tween:Tween
		tween = $image0/tween
# warning-ignore:return_value_discarded
		tween.interpolate_property(imgs[0], "self_modulate", Color(1, 1, 1, 1), Color(1, 1, 1, 0), _effectDuration / 1000.0)
# warning-ignore:return_value_discarded
		tween.start()
		tween = $image1/tween
# warning-ignore:return_value_discarded
		tween.interpolate_property(imgs[1], "self_modulate", Color(1, 1, 1, 0), Color(1, 1, 1, 1), _effectDuration / 1000.0)
# warning-ignore:return_value_discarded
		tween.start()
		pass

func _effectFadeOut():
	if _hasOneSprite():
		var tween = $image0/tween
		tween.interpolate_property(imgs[0], "self_modulate", Color(1, 1, 1, 1), Color(1, 1, 1, 0), _effectDuration / 1000.0)
		tween.start()
		pass
	elif _hasTwoSprite():
		_mov2in1()
		_effectFadeOut()
		pass
	
func _effectShake():
	if _hasOneSprite():
		var sourcePos = self.rect_position
		var time = 0
		var length = _effectDuration
		var time_add = 100
		var shake_range = 10
		var freg = 0.05
		while time < length:
			time += time_add
			var offset = Vector2(
				rand_range(-shake_range, shake_range),
				rand_range(-shake_range, shake_range)
			)
			var newPos = sourcePos + offset
			self.set_position(newPos)
			yield(get_tree().create_timer(freg), "timeout")
			pass
		self.set_position(sourcePos)
		pass
	elif _hasTwoSprite():
		_mov2in1()
		_effectShake()
		pass
	
func _fadeInFinish():
	_mov2in1()
	imgs[0].self_modulate = Color(1, 1, 1, 1)
	imgs[1].self_modulate = Color(1, 1, 1, 1)
	pass
	
func _mov2in1():
	imgs[0].texture = imgs[1].texture
	imgs[1].texture = null
	imgs[1].visible = false
	pass

func _moveBy():
	var tween = $move
	tween.interpolate_property(self, "rect_position", self.rect_position, self.rect_position+_moveByPoint, _effectDuration / 1000.0)
	tween.start()
	pass
	
func _scaleTo():
	var tween = $scale
	tween.interpolate_property(self, "rect_scale", self.rect_scale, _scaleToVector, _effectDuration / 1000.0)
	tween.start()
	pass
	
func _rotateBy():
	var tween = $rotation
	tween.interpolate_property(self, "rect_rotation", self.rect_rotation, self.rect_rotation+_rotateByValue, _effectDuration / 1000.0)
	tween.start()
	pass

func getSaveData():
	var r = {
		"type": type(),
		"position": to_json({
			"x": self.rect_position.x,
			"y": self.rect_position.y
		}),
		"scale": to_json({
			"x": self.rect_scale.x,
			"y": self.rect_scale.y
		}),
		"rotation": self.rect_rotation,
		"visible": self.visible,
		"imgName": _imgFile
	}
	return r
	
func setSaveData(data : Dictionary):
	self.loadImage(data.imgName)
	var position = parse_json(data.position)
	self.rect_position = Vector2(position.x, position.y)
	var scale = parse_json(data.scale)
	self.rect_scale = Vector2(scale.x, scale.y)
	self.rect_rotation = data.rotation
	self.visible = data.visible
	
